/* USER CODE BEGIN Header */
/**

  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with 
	this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "st7735.h"
#include "LED.h"
#include "car.h"
#include "Motor.h"
#include "sigan.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define CAR_SPEED 100
#define CAR_ACC 50
#define SIGAN_UPSPEED 50
#define SIGAN_DOWNSPEED 50


static uint8_t DataRcvd_maix;
static uint8_t DataRcvd_QR;
uint8_t DataRcvd_ZDT[4];
uint8_t debug_num;
uint8_t debug_Count = 0;

uint8_t Ready_Flag;
uint8_t Load_Flag;
uint8_t step = 1;
static int index;

uint8_t Car_Counter;
uint8_t Tim_carCounter;

char debug_msg[32];
uint8_t Rx_Buff[]="123+321";
static uint8_t Task_Flag;
static uint8_t num=0;
static uint8_t TASK1_1,TASK1_2,TASK1_3,TASK2_1,TASK2_2,TASK2_3;
uint8_t TASK_INDEX[]={1,2,3,4,0,0,0,0};
static char task_str[8];
uint32_t time=1000;
uint8_t duty = 0;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#include "usart.h"
#include "stdint.h"



void UpdateTaskString(void)//任务更新
{
    task_str[0] = TASK1_1 + '0';  
    task_str[1] = TASK1_2 + '0';
    task_str[2] = TASK1_3 + '0';
    task_str[3] = '+';            
    task_str[4] = TASK2_1 + '0';
    task_str[5] = TASK2_2 + '0';
    task_str[6] = TASK2_3 + '0';
    task_str[7] = '\0';           
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//串口中断回调
 {
//	if(huart==&huart1)
//	{
//		
//		if(DataRcvd_ZDT[3] == 0x6B)
//		{			
//			Ready_Flag=1;
//		}
//			
//		HAL_UART_Receive_IT(&huart1,DataRcvd_ZDT,4);
//	}
	if(huart == &huart4)
	{
//		if(DataRcvd_maix=='1')
//		{
//			time=1000;
//		}
//		else if(DataRcvd_maix=='2')
//		{
//			time=500;
//		}
//		else if(DataRcvd_maix=='3')
//		{
//			time=50;
//			ST7735_FillScreen(ST7735_BLUE);
//		}
		HAL_UART_Receive_IT(&huart4,&DataRcvd_maix,1);
	}
	if (huart == &huart6) 
	{
        switch (num) {
            case 0:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK1_1 = DataRcvd_QR - '0';
                    num++;
                } else num = 0;
                break;
            case 1:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK1_2 = DataRcvd_QR - '0';
                    num++;
                } else num = 0;
                break;
            case 2:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK1_3 = DataRcvd_QR - '0';
                    num++;
                } else num = 0;
                break;
            case 3:
                if (DataRcvd_QR == '+') num++;
                else num = 0;
                break;
            case 4:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK2_1 = DataRcvd_QR - '0';
                    num++;
                } else num = 0;
                break;
            case 5:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK2_2 = DataRcvd_QR - '0';
                    num++;
                } else num = 0;
                break;
            case 6:
                if (DataRcvd_QR >= '1' && DataRcvd_QR <= '3') {
                    TASK2_3 = DataRcvd_QR - '0';
                    UpdateTaskString();
										HAL_UART_Transmit_IT(&huart4,(uint8_t *)task_str,7);
                    ST7735_FillScreen(ST7735_WHITE);
                    ST7735_DrawString(0, 40, task_str, ST7735_BLUE, ST7735_WHITE, &Font_16x64);
                }
                num = 0;
                break;
        }
        HAL_UART_Receive_IT(&huart6, &DataRcvd_QR, 1);

	}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//定时器运行中断回�?
{
	if(htim == &htim6)
	{
		if(Ready_Flag == 0)//电机运行时间中断
		{
			Tim_carCounter++;
			if(Tim_carCounter >= Car_Counter)
			{
				Ready_Flag = 1;
				Tim_carCounter = 0;
			}
		}
	}
}

void VarFlag_Init()
{
	Ready_Flag = 1;
	Task_Flag = 1;
}

void Car_Init()
{
	__HAL_TIM_SET_COMPARE(&htim14,TIM_CHANNEL_ALL,0);
	__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_ALL,0);
  Motor_Init();
	HAL_Delay(500);
  Sigan_Init();
	HAL_Delay(500);
	Sigan_Load(2);
	HAL_Delay(1000);
  JiaZhua_jia();
	HAL_Delay(500);
}


void Control_Proc()
{
	if(Ready_Flag == 1)
		{  
			if(Task_Flag == 1)//出发
			{
				static int temp = 0;
				if(temp == 0)
				{
					Car_Left(20,20,1);  
					Car_Counter = 4;
					Ready_Flag = 0;
					temp = 1;
				}
				else if(temp == 1)
				{
					Car_Go(15,20,2.3);  
					Car_Counter = 12;
					Ready_Flag = 0;
					temp = 2;
				}
				else if(temp == 2)
				{
					Car_Go(80,40,4.3);
					Car_Counter = 8;
					Ready_Flag = 0;
					temp = 3;
				}
				else if(temp == 3)
				{
					Car_Right(40,20,0.3);
					JiaZhua_fang();
					Car_Counter = 3;
					Ready_Flag = 0;
					temp = 4;
				}
				else if(temp == 4)//识别
				{
					Sigan_Turn();
					Car_Counter = 2;
					Ready_Flag = 0;
					temp = 5;
				}
				else if(temp == 5)//拿物料
				{
					Sigan_Down(80,0,0.5);
					HAL_Delay(1000);
					JiaZhua_jia();
					HAL_Delay(1000);
					Sigan_Up(80,0,0.5);
					Car_Counter = 2;
					Ready_Flag = 0;
					temp = 6;
				}
				else if(temp == 6)
				{

					if(TASK_INDEX[index] == 1)
					{
						Sigan_Load(1);
						HAL_Delay(2000);
						Sigan_Down(50,0,0.5);
						HAL_Delay(1000);
						JiaZhua_fang();
						Sigan_Up(80,0,0.5);
						Car_Counter = 2;
						Ready_Flag = 0;
					}
					else if(TASK_INDEX[index] == 2)
					{
						Sigan_Load(2);
						HAL_Delay(2000);
						Sigan_Down(80,0,0.5);
						HAL_Delay(1000);
						JiaZhua_fang();
						Sigan_Up(50,0,0.5);
						Car_Counter = 2;
						Ready_Flag = 0;
					}
					else if(TASK_INDEX[index] == 3)
					{
						Sigan_Load(3);
						HAL_Delay(2000);
						Sigan_Down(80,0,0.5);
						HAL_Delay(1000);
						JiaZhua_fang();
						Sigan_Up(80,0,0.5);
						Car_Counter = 2;
						Ready_Flag = 0;
					}
					index++;
					temp = 7;
				}
				else if(temp == 7)
				{
					Sigan_Turn();
					Car_Counter = 2;
					Ready_Flag = 0;
					if(index>3)
					{
						temp = 8;
					}
					else
					{
						temp = 5;
					}
				}
			}
		}
	else
	{
	}
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI2_Init();
  MX_USART6_UART_Init();
  MX_UART4_Init();
  MX_USART1_UART_Init();
  MX_TIM6_Init();
  MX_TIM14_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  
	HAL_TIM_Base_Start_IT(&htim6);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim14,TIM_CHANNEL_1);
	HAL_UART_Receive_IT(&huart4,&DataRcvd_maix,1);
	HAL_UART_Receive_IT(&huart6,&DataRcvd_QR,1);
  VarFlag_Init();
	HAL_Delay(200);
  ST7735_Init();
	HAL_Delay(200);
  Car_Init();
	HAL_Delay(2000);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		//Sigan_Load(2);
	  
		Control_Proc();
  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
